﻿using System;

using System.Data;
using System.Data.SqlClient;

namespace IFD2
{
    public partial class FltSaleIndex : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["ACL"].ToString().IndexOf("IC1") < 0)
                {
                    Response.Redirect("unauthorized.aspx");
                }

                lblAirline.Text = Session["AIRLINE"].ToString();
                if (Session["FLT_NO"] != null)
                    txtFltNo.Text = Session["FLT_NO"].ToString();
                if (Session["FLT_DATE"] != null)
                    txtFltDate.Text = Convert.ToDateTime(Session["FLT_DATE"].ToString()).ToShortDateString();

                if (Request.QueryString["flt_no"] != null)
                {
                    txtFltNo.Text = Request.QueryString["flt_no"].ToString();
                    txtFltDate.Text = Convert.ToDateTime(Request.QueryString["flt_date"].ToString()).ToShortDateString();
                }
            }
        }

        protected void lbtnQuery_Click(object sender, EventArgs e)
        {
            try
            {
                DateTime.Parse(txtFltDate.Text);
            }
            catch
            {
                lblError.Text = "Invalid date format!";
                return;
            }

            Session["FLT_NO"] = txtFltNo.Text;
            Session["FLT_DATE"] = txtFltDate.Text;

            int ret_code;
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "bll_flt_get_sale_income_per_pax_km";
            SqlParameter para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
            para = cmd.Parameters.AddWithValue("@flt_no", txtFltNo.Text);
            para = cmd.Parameters.AddWithValue("@flt_date", txtFltDate.Text);
            para = cmd.Parameters.AddWithValue("@currency", lstCurrencies.SelectedValue);
            para = cmd.Parameters.Add("@total_pax", SqlDbType.SmallInt);
            para.Direction = ParameterDirection.Output;
            SqlParameter para2 = cmd.Parameters.Add("@index", SqlDbType.SmallMoney);
            para2.Direction = ParameterDirection.Output;
            ret_code = TinyDAL.Exec(ref cmd);
            if (ret_code == 0)
            {
                lblError.Text = "Done";
                lblTotalPax.Text = para.Value.ToString();
                lblIndex1.Text = lstCurrencies.SelectedValue + para2.Value.ToString();
            }
            else if (ret_code == 1)
            {
                lblError.Text = "Invalid date period!";
                return;
            }
            else
            {
                lblError.Text = "Error:" + ret_code.ToString();
                return;
            }

            cmd.Parameters.Clear();
            cmd.CommandText = "bll_flt_get_sale_income_per_seat_km";
            para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
            para = cmd.Parameters.AddWithValue("@flt_no", txtFltNo.Text);
            para = cmd.Parameters.AddWithValue("@flt_date", txtFltDate.Text);
            para = cmd.Parameters.AddWithValue("@currency", lstCurrencies.SelectedValue);
            para = cmd.Parameters.Add("@capacity", SqlDbType.SmallInt);
            para.Direction = ParameterDirection.Output;
            para2 = cmd.Parameters.Add("@index", SqlDbType.SmallMoney);
            para2.Direction = ParameterDirection.Output;
            ret_code = TinyDAL.Exec(ref cmd);
            if (ret_code == 0)
            {
                lblError.Text = "Done";
                lblCapacity.Text = para.Value.ToString();
                lblIndex2.Text = lstCurrencies.SelectedValue + para2.Value.ToString();
            }
            else if (ret_code == 1)
            {
                lblError.Text = "Invalid date period!";
                return;
            }
            else
            {
                lblError.Text = "Error:" + ret_code.ToString();
                return;
            }
        }
    }
}